<%-- 
Copyright [2008] [Manuel Kraft] 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software distributed under
 the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and limitations under the License.

Author     : cutmasta@gmail.com
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@ page import="org.cutmasta.ip.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="java.util.logging.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>


<%
            if (request.getParameter("action") != null && request.getParameter("action").equals("logout")) {
                HttpSession http_session = request.getSession();
                http_session.removeAttribute("user");
                session.invalidate();
                response.sendRedirect(response.encodeRedirectURL("login.jsp"));
            }


%>


<html>
    <head>
        <title>OX IP</title>
        <meta id="viewport" name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"></meta>
        <link rel="stylesheet" href="mystyle.css" type="text/css"></link>
    </head>
    <body onload="setTimeout(scrollTo, 0, 0, 1)">
        <div class="toolbar1 chrome"></div>
        
        <div selected="true" class="fullPage chrome">
            
            <form action='<%= response.encodeURL("login.jsp") %>' method="POST" class="bigTextForm">
                
                
                <%


            if (request.getParameter("user") != null && request.getParameter("user").length() > 0 && request.getParameter("passwd") != null && request.getParameter("passwd").length() > 0) {

                // the datasource ant the MultiThreadedHttpConnectionManager should be initialized via a servlet init class which listens to the init of a context
                // see http://www.onjava.com/pub/a/onjava/2006/04/19/database-connection-pooling-with-tomcat.html
                Context initCtx = new InitialContext();
                Context envCtx = (Context) initCtx.lookup("java:comp/env");

                // Look up our data source
                DataSource ds = (DataSource) envCtx.lookup("jdbc/oxip");

                // Allocate and use a connection from the pool
                Connection conn = null;
                ResultSet rs = null;
                PreparedStatement ps = null;

                try {
                    conn = ds.getConnection();
                    ps = conn.prepareStatement("select username,password,oxurl,contact_id from test where username = ?");
                    ps.setString(1, request.getParameter("user").trim());
                    rs = ps.executeQuery();

                    String username = null;
                    String password = null;
                    String oxurl = null;
                    int contact_folder_id = 6;

                    boolean valid = false;

                    if (rs.next()) {
                        valid = true;
                        username = rs.getString("username");
                        password = rs.getString("password");
                        oxurl = rs.getString("oxurl");
                        contact_folder_id = rs.getInt("contact_id");
                    }

                    // set to always true, use ox to auth
                    //valid = true;
                    //oxurl = "ox6.netline-is.de";
                    username = request.getParameter("user").trim();
                    password = request.getParameter("passwd").trim();


                    // if ok redirect to main site
                    if (valid) {
                        HttpSession http_session = request.getSession(true);
                        User s = new User();
                        s.setUsername(username);
                        s.setPassword(password);
                        s.setOxurl(oxurl);
                        s.setContactFolder(contact_folder_id);
                        if (s.getContactFolder() == 0) {
                            s.setContactFolder(6); // default global addrerssbuch
                        }

                        http_session.setAttribute("user", s);
                        response.sendRedirect(response.encodeRedirectURL("index.jsp"));
                    }


                } catch (SQLException sql) {
                    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, sql);
                } finally {
                    if (rs != null) {
                        rs.close();
                    }
                    if (ps != null) {
                        ps.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                }

            }


            out.println("<div class=\"label\">Username:</div>");

            if (request.getParameter("user") != null) {
                out.println("<input name=\"user\" type=\"text\" value=\"" + request.getParameter("user") + "\"/>");            
            } else {                
                out.println("<input autocorrect =\"off\"  autocapitalize=\"off\" type=\"text\" name=\"user\" value=\"\"> ");
            }


            out.println("<div class=\"label\">Password:</div>");
            if (request.getParameter("passwd") != null) {
                out.println("<input name=\"passwd\" type=\"password\" value=\"" + request.getParameter("passwd") + "\"/>");
                
            } else {                
                out.println("<input autocorrect =\"off\"  autocapitalize=\"off\" type=\"password\" name=\"passwd\" value=\"\">");
            }
                
                
                %>
                
                
                
                <input type="hidden" name="action" value="do_login">
                <input type="submit" name="login" value="Anmelden" class="bigButton"></input>
            </form>
            <div class="toolbar1 chrome"></div>
            <div style="padding:10px;"><a href="http://www.androidportal.com">Android Development Portal</a></div>
            
        </div>
        
    </body>
    
    <div id="tabconsole"></div>
    
</html>

